[R语言] RStudio美化

这是一篇关于RStudio美化的小技巧

Posted by Leung ZhengHua on 2017-11-11

本文总点击量

help page 输出文档美化

这个美化文档的最初想法来自Raining_wcc,后来在他的CSS上大刀阔斧地又调出了符合本人审美的CSS,一个特点就是字体比较大。

将下列文件保存为R.css文件,替换到Rstudio安装文件夹下的RStudio\resources\R.css

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
* R.css
*
* Copyright (C) 2009-11 by RStudio, Inc.
*
* Unless you have received this program directly from RStudio pursuant
* to the terms of a commercial license agreement with RStudio, then
* this program is licensed to you under the terms of version 3 of the
* GNU Affero General Public License. This program is distributed WITHOUT
* ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
* AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
*
*/
body, td {
font-family: sans-serif;
font-size: 10pt;/*lzh*/
}
::selection {
background: #1d953f; /*lzh 控制选中文本的区域颜色*/
}
::-moz-selection{
background: rgb(181, 213, 255);
}
a:visited {
color: rgb(50%, 0%, 50%);
}
h1 {
font-size: x-large;
color: #DAA520; /*lzh 控制package目录下首行一级标题颜色*/
/*lzh additional advise : under the path 'R-3.2.2\doc\html\logo.jpg'. if you delete this img then no image will be showed in h1 level*/
}
h2 {
font-size: x-large;
font-weight: normal;
}
h3 {
color: rgb(35%, 35%, 35%);
}
h4 {
color: rgb(35%, 35%, 35%);
font-style: italic;
}
h5 {
color: rgb(35%, 35%, 35%);
}
h6 {
color: rgb(35%, 35%, 35%);
font-style: italic;
}
img.toplogo {
vertical-align: middle;
}
img.arrow {
width: 30px; /*lzh*/
height: 30px;/*lzh*/
border: 0;
}
span.acronym {
font-size: small;
}
span.env {
font-family: monospace;
}
span.file {
font-family: monospace;
}
span.option {
font-family: monospace;
}
span.pkg {
font-weight: bold;
}
span.samp {
font-family: monospace;
}
div.vignettes a:hover {
background: rgb(85%, 85%, 85%);
}
table p {
margin-top: 0;
margin-bottom: 6px;
}
/* Below is my customization to the help page appearance */
/*==============================
============================*/
body{
background-color: #2F4F4F; /*lzh 整体背景颜色*/
}
body, td{
font-size: 15px !important; /*lzh控制正文字体大小*/
font-family: "Segoe UI" !important;
color: #AFEEEE; /*正文颜色*/
}
pre, code, table tr td:nth-child(1){
font-family: "Consolas" !important;
}
code{
border: 1px solid #708090; /*lzh 控制关键字的边界*/
border-radius: 5px; /*lzh 控制关键字背景圆润程度*/
color: #DAA520;/*LZH 控制关键字但不是可跳转函数的颜色*/
background-color: #708090; /*lzh 控制关键字和see also的背景色*/
padding: 0px 2px;/*lzh 控制关键字底部背景长宽*/
}
a{
text-decoration: none;
color: #00FF00;/*lzh 能够跳转的函数和函数目录中的可跳转函数颜色*/
font-size: 12pt; /*lzh control help page's link function*/
}
a:hover, a:focus{
color: #DAA520;/*lzh 控制光标所到之处关键函数的色彩变换*/
text-decoration: underline;
}
pre{
padding: 4px;/*LZH 控制usage example中居中,越大,四周空白边缘越大*/
border-style: solid;
border-width: 6px 6px 6px 6px; /*lzh usage 左端修饰梯形边大小*/
border-color: #E6E6FA;/*lzh 控制矩形四周的装饰边颜色*/
border-left-color: #418AB5;
background-color: #E6E6FA; /*lzh usage example底色*/
/* color: #005580; */
}
body>table:nth-child(1){
border: 1px solid #BDB76B;/*lzh 文档顶部条形边界底色*/
border-radius: 20px;/*lzh 文档顶部条形边界圆润程度*/
background-color: #BDB76B; /*lzh 文档顶部条形底色*/
padding: 4px 8px;/*lzh 控制顶部条形的宽度*/
}
div, body>table:nth-child(1) td{
font-style: italic;
font-size: 16pt !important;/*lzh 控制顶部条形文字和底部index的大小*/
color: black; /*lzh 控制顶部标题字体颜色*/
/*text-shadow: 1px 1px 0px #555555;/*lzh 顶部阴影*/
}
div{
color: #6495ED;/*lzh 最底部中间的index颜色*/
}
h2 {
font-weight: 600 !important;
margin-top: 0.2em;
padding-bottom: 10px;
border-bottom: 1px solid #DDE4E6;
color: #418AB5;
}
h3 {
font-weight: 600;
text-transform: uppercase;
margin-top: 1.5em;
margin-bottom: 0.6em;
color: #00FFFF; /*lzh usage deacription 的标题颜色*/
}
h3 ~ p, h3 ~ table, h3+* table {
/* margin-left: 20px; */
padding-left: 20px;
}
h3 ~ pre{
margin-left: 20px;
color: #181d4d;
font-size: 11pt; /*控制usage和EXAMPLES的代码颜色和大小*/
}
h3 + *{
margin-top: 0px;
}
dt {
font-style: italic;
}
p {
line-height: 24px;
color: #AFEEEE; /*正文颜色*/
font-size: 16px;
}
hr{
border-style: hidden;
}
table[summary$="block"] p {
margin-bottom: 16px;
}
table[summary$="block"] td:nth-child(1) {
padding-right: 16px;
}
table[summary$="block"] td:nth-child(1) code{
padding-right: 16px; /*lzh*/
border: none;
background: none;
font-weight: bold;
color: #FF8C00; /*输入参数ARGUMENTS和value输出参数的颜色*/
font-size: 16px;
}
table[summary$="block"] tr {
vertical-align: top;
}
table[summary$="block"] td:nth-child(2){
margin: 0px;
}
h2 + table tr:nth-child(even), dl table tr:nth-child(even){
background-color: #2F4F4F; /*lzh package原始目录下所有函数描述的表格的间隔颜色*/
}
h2 + table, dl table{
border-spacing: 0px 2px;
}
h2 + table{
width: 100%;
padding-left: 20px;
}

效果如图:

帮助文档的头部常常会有一个R图标,我觉得删去这张图之后会更美观一点,它位于安装的R文件夹下的R-3.2.2\doc\html下,其他图标(比如向左向右的图标)也在这个文件夹下面。

美化html输出文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
---
title: '<div class="centerTXT">短暂的婚姻</div>'
author:
- name: '<div class="centerTXT"><br> </br> <br> </br>梁政华<br> </br></div>'
email: '<div class="centerTXT">liangzhenghua@foxmail.com</div>'
affiliation: '<div class="centerTXT">中央财经大学</div>'
date: '<div class="centerTXT"> 2017年10月24日 </div> <br> </br> <br> </br> <br> </br>'
abstract: |
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur eget porta erat. Morbi consectetur est vel gravida pretium. Suspendisse ut dui eu ante cursus gravida non sed sem. Nullam sapien tellus, commodo id velit id, eleifend volutpat quam. Phasellus mauris velit, dapibus finibus elementum vel, pulvinar non tellus. Nunc pellentesque pretium diam, quis maximus dolor faucibus id. Nunc convallis sodales ante, ut ullamcorper est egestas vitae. Nam sit amet enim ultrices, ultrices elit pulvinar, volutpat risus.
It consists of two paragraphs.
output:
html_document:
self_contained: false
theme: sandstone
highlight: haddock
toc: true
number_sections: true
toc_float:
collapsed: true
smooth_scroll: false
toc_depth: 3
fig_width: 7
fig_height: 6
fig_caption: true
df_print: paged
code_folding: hide
keep_md: false
code_download: true
---
 
<style type="text/css">
body{
font-size: 15px;
background: #eee;
font-family:Georgia, Palatino, 'Palatino Linotype', Times, 'Times New Roman', serif;
}
td {
font-size: 8px;
}
.centerTXT{text-align:center;}
h1 {
font-size: 28px;
color: DarkBlue;
display:block;
border-bottom:0px solid #999;
background:#F5DEB3;
text-align: center;
margin: 20px auto;
}
h2 {
font-size: 22px;
color: DarkBlue;
border-bottom:0px solid #999;
}
h3 {
font-size: 18px;
color: DarkBlue;
}
code.r{
font-size: 14px;
}
pre {
font-size: 12px
}
a{ color: #0645ad; cursor:pointer;}
a:visited{ color: #0b0080; }
a:hover{ color: #06e; }
a:active{ color:#faa700; }
a:focus{ outline: thin dotted; }
a:hover, a:active{ outline: 0; }
::-moz-selection{background:rgba(255,255,0,0.3);color:#000}
::selection{background:rgba(255,255,0,0.3);color:#000}
a::-moz-selection{background:rgba(255,255,0,0.3);color:#0645ad}
a::selection{background:rgba(255,255,0,0.3);color:#0645ad}
blockquote{
color:#000079;
margin:0;
padding-left: 1em;
border-left: 0.5em #636363 solid;
}
</style>
-----------------
你可以在这一块为所欲为
加入一张图提示结束也是很不错的样子
****
![](http://i3.s.7.hjfile.cn/entry/201505/2b214877-b6df-44c9-8136-93b7e4f0f313.jpg)
------------------

在Rmd文件头部填入上述代码,可以生成美观的HTML文档,但依赖于js渲染,因此生成html文件的时候会同时产生一个相应的文件夹。

另一种简易的方法是安装prettydoc的R包之后,在Rmd文档头部加入:

1
2
3
4
5
6
7
8
---
title: "月份效应与行业效应"
output:
prettydoc::html_pretty:
theme: cayman
highlight: github
toc: true
---